package com.zhongzhi.javafx.controller.updata;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.Resource;

import org.apache.catalina.connector.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.zhongzhi.common.Common;
import com.zhongzhi.common.CommonUpdata;
import com.zhongzhi.service.TypeService;
import com.zhongzhi.serviceimpl.EquipmentServiceImpl;
import com.zhongzhi.serviceimpl.TypeServiceImpl;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.Window;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;

@Log4j
@FXMLController
public class TypeUpdataViewController implements Initializable{
	@FXML
	private Label text11;
	@FXML
	private TextField typeName;
	@FXML
	private Label text1;
	@FXML
	private TextArea remarks;
	@FXML
	private Button stop;
	@FXML
	private Button add;
	@FXML
	private Label textts;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("修改机型窗口:开始初始化");
		add.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if (typeName.getText()==null || typeName.getText().equals("")) {
					textts.setText("机型名称不能为空");
				}else{
					if (remarks.getText()==null || remarks.getText().equals("")) {
						remarks.setText("无");
					}
					insert(typeName.getText(),remarks.getText());
				}
				
				
			}
		});

		stop.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}
		});
		Type type = CommonUpdata.getType();
		typeName.setText(type.getTypeName());
		remarks.setText(type.getRemarks());
		if (type.getRemarks()==null || type.getRemarks().equals("")) {
			remarks.setText("无");
		}else {
			remarks.setText(type.getRemarks());
		}
		log.info("修改机型窗口:初始化完毕");

	}

	//插入数据库
	private void insert(String typeName,String remarks) {
		try {
			Type type = CommonUpdata.getType();
			String s = "http://127.0.0.1:8080/type/update?id="+type.getId()+"&typeName="+typeName.trim()+"&remarks="+remarks.trim();
			log.info("修改机型窗口:插入数据库:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("修改机型窗口:插入数据库结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			if (jsonobj.getCode()==200) {
				ScrollPane sp = Common.getSp();
				sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Type.fxml")));
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}else {
				textts.setText(jsonobj.getMessage());
			}
		} catch (Exception e) {
			log.error("修改机型窗口:插入数据库失败:"+e);
		}
		
	}

}
